/********************************************************************/
/*************** 为Base*.vue正常执行逻辑提供的常用的方法 ***************/
/********************************************************************/

import dayjs from "dayjs";
import { needParam } from "flc-base-utils";
import _ from "lodash";
import { showMessage } from "./platform";

/**
 * 获取时间阶段
 */
export function getTimePeriodAlias() {
  const hours = new Date().getHours();
  if (hours < 1) return "凌晨";
  if (hours < 8) return "清晨";
  if (hours < 11) return "上午";
  if (hours < 13) return "中午";
  if (hours < 18) return "下午";
  if (hours < 20) return "傍晚";
  if (hours < 23) return "晚上";
  return "深夜";
}

/**
 * 获取时间（几年/月/天/小时/分钟前）
 * @param {any} time 要处理的时间
 */
export function getTimeAgo(time: any = needParam()) {
  if (!time) return "";
  time = dayjs().diff(time) / 1000;
  const y = Math.floor(time / (60 * 60 * 24 * 365));
  if (y > 0) return `${y}年前`;
  const M = Math.floor(time / (60 * 60 * 24 * 30));
  if (M > 0) return `${M}月前`;
  const d = Math.floor(time / (60 * 60 * 24));
  if (d > 0) return `${d}天前`;
  const h = Math.floor(time / (60 * 60));
  if (h > 0) return `${h}小时前`;
  const m = Math.floor(time / 60);
  if (m > 0) return `${m}分钟前`;
  return "刚刚";
}

/**
 * 获取数量文本
 * @param {string | number}  num 要处理的数字
 * @returns {string}
 */
export function getAmountText(num: number | string, n = 1) {
  if (typeof num === "string") num = parseInt(num);
  if (num < 10000) return num.toString();
  num = num / 10000;
  if (num < 1000) return num.toFixed(n) + "w";
  return (num / 1000).toFixed(n) + "kw";
}

/**
 * 计算弹性布局时，末尾需要的空盒子个数
 * @param {number} total  总共多少个元素
 * @param {number} cols   多少列布局
 * @returns {number}
 */
export function getEmptyNum(total: number, cols: number): number {
  return cols - (total % cols);
}

/**
 * 复制文本
 * @param {string} text 要复制到剪切板的内容
 */
export function copyText(text = "") {
  const input = document.createElement("input");
  input.setAttribute("value", text);
  document.body.appendChild(input);
  input.select();
  const copyText = document.execCommand("copy");
  document.body.removeChild(input);
  if (copyText) showMessage("复制成功！");
}
